//
//  HXCachTools.m
//  A段项目  穷游
//
//  Created by 韩旭 on 16/6/14.
//  Copyright © 2016年 基础. All rights reserved.
//

#import "HXCachTools.h"

@implementation HXCachTools
+ (void)archiverObject:(id)obkect ByKey:(NSString *)key WithPath:(NSString *)path{
    //初始化存储对象信息的data
    NSMutableData *data = [NSMutableData data];
    //创建归档工具对象:
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    //开始归档:
    [archiver encodeObject:obkect forKey:key];
    //结束归档
    [archiver finishEncoding];
    //写入本地
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
    NSString *destPath = [[[[docPath stringByAppendingPathComponent:@"Caches"] stringByAppendingPathComponent:@"default"] stringByAppendingPathComponent:@"com.hackemist.SDWebImageCache.default"] stringByAppendingPathComponent:path];
    [data writeToFile:destPath atomically:YES];
}

+ (id)unarchiverObjectByKey:(NSString *)key WithPath:(NSString *)path{
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
    NSString *destPath = [[[[docPath stringByAppendingPathComponent:@"Caches"] stringByAppendingPathComponent:@"default"] stringByAppendingPathComponent:@"com.hackemist.SDWebImageCache.default"] stringByAppendingPathComponent:path];
    NSData *data = [NSData dataWithContentsOfFile:destPath];
    //创建反归档对象:
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    //接收反归档得到的对象:
    id object = [unarchiver decodeObjectForKey:key];
    return object;
}

@end
